﻿using System.IO;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.VisualBasic.Devices;
using Microsoft.VisualBasic.FileIO;
using System.Collections.Generic; // not strictly necessary, but shortens the access to some options

namespace LuaBoard
{
    public class FileCopyData
    {
        private string _sourcePath;
        private string _distPath;
        private bool _overwrite;

        public FileCopyData(string sourcePath, string distPath, bool overwite)
        {
            _sourcePath = SourcePath;
            _distPath = DistPath;
            _overwrite = Overwite;
        }
        public string SourcePath
        {
            get { return _sourcePath; }
        }
        public string DistPath
        {
            get { return _distPath; }
        }
        public string Overwite
        {
            get { return _overwrite; }
        }
    }
    public class FileCopy
    {
        public List<FileCopyData> fileCopyData = new List<FileCopyData>();

        private void BackgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {

            string[] theExtentions = { "*.jpg", "*.bmp", "*.png", "*.gif" };

            foreach (string currentExt in theExtentions)
            {

                string[] theFiles = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), currentExt, System.IO.SearchOption.AllDirectories);

                BeginInvoke(_ProBar, new object[] { theFiles.Length });

                foreach (string currentFile in theFiles)
                {

                    try
                    {

                        Computer c = new Computer();

                        c.FileSystem.CopyFile(currentFile, "c:\\tempfiles\\" + System.IO.Path.GetFileName(currentFile), true);

                        BeginInvoke(_StepBar);

                    }

                    catch (Exception ex)
                    {

                        // if any other error type occurs or the attempts are

                        // too many, do the following.

                        MessageBox.Show(ex.Message);

                        break; // TODO: might not be correct. Was : Exit Try

                    }

                }

                BeginInvoke(_ValueBar, new object[] { 1 });

            }

        }

        delegate void ProBar(int value);

        delegate void StepBar();

        delegate void ValueBar(int value);

        private void UpProBar(int value)
        {

            this.progressBar1.Maximum = value;

        }

        private void UpStepBar()
        {

            try
            {

                progressBar1.PerformStep();

            }

            catch (Exception ex)
            {

                // if any other error type occurs or the attempts are

                // too many, do the following.

                MessageBox.Show(ex.Message);

                //break; // TODO: might not be correct. Was : Exit Try

            }

        }

        private void UpValueBar(int value)
        {

            this.progressBar1.Value = value;

        }



        private void BackgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {

            this.progressBar1.Visible = false;

            this.button1.Text = "Copy";

        }



        private void button1_Click(object sender, EventArgs e)
        {

            try
            {

                if (this.button1.Text == "Copy")
                {

                    this.button1.Text = "Cancel";

                    _ProBar = new ProBar(UpProBar);

                    _StepBar = new StepBar(UpStepBar);

                    _ValueBar = new ValueBar(UpValueBar);

                    this.progressBar1.Minimum = 1;

                    this.progressBar1.Value = 1;

                    this.progressBar1.Step = 1;

                    this.progressBar1.Visible = true;

                    this.backgroundWorker1.RunWorkerAsync();

                }

                else if ((this.button1.Text == "Cancel"))
                {

                    this.backgroundWorker1.CancelAsync();

                    this.progressBar1.Visible = false;

                    this.button1.Text = "Copy";

                    MessageBox.Show("Cancelled");

                }

            }

            catch (Exception ex)
            {

                // if any other error type occurs or the attempts are

                // too many, do the following.

                MessageBox.Show(ex.Message);

                //break; // TODO: might not be correct. Was : Exit Try

            }

        }
    }

}
